package com.github.linkoog.model_designer.ide.editer.dbtable.tab_editer;

import com.intellij.codeHighlighting.BackgroundEditorHighlighter;
import com.intellij.ide.structureView.StructureViewBuilder;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorLocation;
import com.intellij.openapi.fileEditor.FileEditorState;
import com.intellij.openapi.fileEditor.FileEditorStateLevel;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Splitter;
import com.intellij.openapi.util.UserDataHolderBase;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.beans.PropertyChangeListener;

public class DBTableUIFormEditor extends UserDataHolderBase implements FileEditor {

    public static final FileEditorState MY_EDITOR_STATE = (otherState, level) -> false;

    private final Project project;
    private final VirtualFile virtualFile;


    private JPanel myMainPanel;
    private final DBTableLeftPanel dbTableUI;

    public DBTableUIFormEditor(Project project, VirtualFile virtualFile){
        this.project = project;
        this.virtualFile = virtualFile;
        this.dbTableUI = new DBTableLeftPanel(project, virtualFile);

        myMainPanel = dbTableUI.getMyMainPanel();
        initUi();
    }

    private void initUi(){
        this.myMainPanel = new JPanel();
        this.myMainPanel.setLayout(new BorderLayout(0, 0));


        // 左右分割面板
        Splitter splitter = new Splitter(false, 0.75F);
        splitter.setResizeEnabled(false);
        splitter.setDividerPositionStrategy(Splitter.DividerPositionStrategy.KEEP_FIRST_SIZE);
        splitter.setFirstComponent(dbTableUI.getMyMainPanel());
        splitter.setSecondComponent(new JPanel());

        this.myMainPanel.add(splitter, BorderLayout.CENTER);
    }


    @Override
    public JComponent getComponent() {
        return this.myMainPanel;
    }

    @Override
    public JComponent getPreferredFocusedComponent() {
        return null;
    }

    @Override
    public void dispose() {
        if (dbTableUI != null) {
            dbTableUI.dispose();
        }
    }

    @Override
    public VirtualFile getFile() {
        return virtualFile;
    }



    @Override
    public String getName() {
        return "UI Editer";
    }



    @Override
    public boolean isModified() {
        return false;
    }

    @Override
    public boolean isValid() {
        return true;
    }

    @Override
    public void selectNotify() {
        dbTableUI.resetPanelContext();
    }

    @Override
    public void deselectNotify() {
        dbTableUI.deselectNotify();
    }

    @Override
    public void addPropertyChangeListener(@NotNull final PropertyChangeListener listener) {
    }

    @Override
    public void removePropertyChangeListener(@NotNull final PropertyChangeListener listener) {
    }

    @Override
    public BackgroundEditorHighlighter getBackgroundHighlighter() {
        return null;
    }

    @Override
    public FileEditorLocation getCurrentLocation() {
        return null;
    }

    @Override
    @NotNull
    public FileEditorState getState(@NotNull final FileEditorStateLevel ignored) {
        return MY_EDITOR_STATE;
    }

    @Override
    public void setState(@NotNull final FileEditorState state) {
    }

    @Override
    public StructureViewBuilder getStructureViewBuilder() {
        return null;
    }


}
